package com.jrfid.smartcabinet.demo.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.ShapeDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.content.res.ResourcesCompat
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SizeUtils
import com.jrfid.smartcabinet.demo.R

class MMenuItemView : LinearLayoutCompat {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initAttr(context, attrs, defStyleAttr)
        initView()
    }

    private var text: String = ""
    private var textSize: Int = SizeUtils.sp2px(16f)
    private var textColor: Int = Color.WHITE
    private var icon: Int = R.drawable.ic_baseline_archive_48
    private var iconSize: Int = SizeUtils.dp2px(48f)
    private var iconPadding: Int = SizeUtils.dp2px(10f)
    private var bgColor: Int = Color.BLUE
    private var corner: Int = SizeUtils.dp2px(6f)

    private fun initAttr(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.MMenuItemView)
        text = a.getString(R.styleable.MMenuItemView_text) ?: ""
        textSize = a.getDimensionPixelSize(R.styleable.MMenuItemView_textSize, SizeUtils.sp2px(16f))
        textColor = a.getColor(R.styleable.MMenuItemView_textColor, Color.WHITE)
        icon = a.getResourceId(R.styleable.MMenuItemView_icon, R.drawable.ic_baseline_archive_48)
        iconSize = a.getDimensionPixelSize(R.styleable.MMenuItemView_iconSize, SizeUtils.dp2px(48f))
        iconPadding = a.getDimensionPixelSize(R.styleable.MMenuItemView_iconPadding, SizeUtils.dp2px(10f))
        bgColor = a.getColor(R.styleable.MMenuItemView_backgroundColor, Color.BLUE)
        corner = a.getDimensionPixelSize(R.styleable.MMenuItemView_cornerRadius, SizeUtils.dp2px(6f))
        a.recycle()
    }

    private fun initView() {
        val textView = TextView(context).apply {
            this.text = this@MMenuItemView.text
            this.setTextColor(this@MMenuItemView.textColor)
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, this@MMenuItemView.textSize.toFloat())
            val drawable = ResourcesCompat.getDrawable(resources, icon, null)
            drawable?.setBounds(0, 0, iconSize, iconSize)
            drawable?.let {
                this.setCompoundDrawables(null, it, null, null)
            }
            this.compoundDrawablePadding = iconPadding
            this.gravity = Gravity.CENTER
        }
        addView(textView)
        orientation = VERTICAL
        gravity = Gravity.CENTER
        val drawable = GradientDrawable().apply {
            this.setColor(bgColor)
            this.cornerRadius = corner.toFloat()
        }
        background = drawable
    }


}